package com.lanou3g.code0517;

import common.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    private Controller controller =new Controller();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取数据

        String username=request.getParameter("username");
        String password =request.getParameter("password");
        String word =request.getParameter("word");

        String msg = "";
        User user =controller.login(username,password);
        if (user!=null){
            msg="登录成功";
            //把user存入session之前,把对象中的密码置空
            user.setPwd("");
            HttpSession session =request.getSession();
            session.setAttribute(Constant.Session.User,user);
        }else {
            msg="登录失败";
        }
        //校验

        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/user/msg.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     this.doPost(request,response);
    }
}
